package com.nurese.controller;

import com.nurese.entity.Careworker;
import com.nurese.entity.User;
import com.nurese.service.CareworkerService;
import com.nurese.service.UserService;
import com.nurese.utils.MapControll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/careworker")
public class CareworkerController {

    private final String LIST="Careworker/list";
    private final String ADD="Careworker/add";
    private final String UPDATE="Careworker/update";

    @Autowired
    private CareworkerService careworkerService;

    @GetMapping("/add")
    public String create(){
        return ADD;
    }

    @PostMapping("/create")
    @ResponseBody
//    public void  create(Careworker careworker){
//        careworkerService.create(careworker);
//    }
    public Map<String,Object> create(Careworker careworker){
//        careworkerService.create(careworker);
        int result= careworkerService.create(careworker);
        if (result<=0){
//            新增失败
            return MapControll.getInstance().error().getMap();

        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = careworkerService.delete(id);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = careworkerService.delete(ids);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/update")
    @ResponseBody
//    @RequestBody
    public Map<String,Object> update(Careworker careworker){
        int result = careworkerService.update(careworker);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @GetMapping ("/detail/{id}")
    public String detail(@PathVariable("id") Integer id,ModelMap modelMap){

        Careworker careworker=careworkerService.detail(id);
        modelMap.addAttribute("careworker",careworker);
        return UPDATE;
    }



    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Careworker careworker){
        List<Careworker> list =careworkerService.query(careworker);
        Integer count=careworkerService.count(careworker);
        return MapControll.getInstance().success().put("data",list).put("count",count).getMap();
    }

    @GetMapping("/list")
    public String list(){

        return LIST;
    }

}

